<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use library\Controller;
use think\Db;
use think\facade\Request;
use \app\admin\model\Users as User_m;

/**
 * 会员管理
 * Class Users
 * @package app\admin\controller
 */
class Users extends Controller
{

    /**
     * 指定当前数据表
     * @var string
     */
    protected $table = 'fd_user';

    /**
     * 会员列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '会员列表';
        $keyword = input('get.keyword/s','');
        $phone = input('get.phone/s','');
        $start_time = Request::get('start_time');
        $end_time = Request::get('end_time');
        $pid_name = Request::get('pid_name');
        $this->assign('pid_name',$pid_name);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('start_times', date("Y-m-d"));
        $this->assign('end_times', date("Y-m-d"));
        if ($pid_name){
            $agent_id = Db::name('fd_user')->where('username',$pid_name)->value('id');
        }
        if ($keyword || $phone || $start_time || $end_time || $pid_name){
            if(!empty($keyword)){
                $where['username'] = $keyword;
            }
            if(!empty($phone)){
                $where['phone'] = $phone;
            }
            if(!empty($pid_name)){
                $where['pid'] = $agent_id;
            }
            
            if(!empty($start_time) && !empty($end_time)){
                $s_time = strtotime($start_time);
                $e_time = strtotime($end_time) + 1 * 24 * 3600;
                
                $where['create_time'] = ['>',$s_time];
                $where['create_time'] = ['<',$e_time];
            }
            
            $user_list = Db::name('fd_user')->where($where)->paginate(50,false,['query'=>request()->param()]);
            $page = $user_list->render();  //构造分页
            $this->assign('pages', $page);   //输出分页
            $this->assign('list',$user_list);
        }else{
            $user_list = Db::name('fd_user')->order('id','desc')
                ->paginate(50,false,['query'=>request()->param()]);
            $page = $user_list->render();  //构造分页
            $this->assign('pages', $page);   //输出分页
            $this->assign('list',$user_list);
        }
        User_m::day_order();
        return $this->fetch();
    }
    
    public function indexs()
    {
        $this->title = '会员列表';
        $keyword = input('get.keyword/s','');
        $phone = input('get.phone/s','');
        $start_time = Request::get('start_time');
        $end_time = Request::get('end_time');
        $pid_name = Request::get('pid_name');
        $this->assign('pid_name',$pid_name);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('start_times', date("Y-m-d"));
        $this->assign('end_times', date("Y-m-d"));
        if ($pid_name){
            $agent_id = Db::name('fd_user')->where('username',$pid_name)->value('id');
        }
        if ($keyword || $phone || $start_time || $end_time || $pid_name){
            if(!empty($keyword)){
                $where['username'] = $keyword;
            }
            if(!empty($phone)){
                $where['phone'] = $phone;
            }
            if(!empty($pid_name)){
                $where['pid'] = $pid_name;
            }
            
            if(!empty($start_time) && !empty($end_time)){
                $s_time = strtotime($start_time);
                $e_time = strtotime($end_time) + 1 * 24 * 3600;
                
                $where['create_time'] = ['>',$s_time];
                $where['create_time'] = ['<',$e_time];
            }
            
            $user_list = Db::name('fd_user')->where($where)->paginate(10,false,['query'=>request()->param()]);
            $page = $user_list->render();  //构造分页
            $this->assign('pages', $page);   //输出分页
            $this->assign('list',$user_list);
        }else{
            $user_list = Db::name('fd_user')->order('id','desc')
                ->paginate(10,false,['query'=>request()->param()]);
            $page = $user_list->render();  //构造分页
            $this->assign('pages', $page);   //输出分页
            $this->assign('list',$user_list);
        }
        User_m::day_order();
        return $this->fetch();
    }
    
    public function indexss()
    {
        $this->title = '会员列表';
        $keyword = input('get.keyword/s','');
        $phone = input('get.phone/s','');
        $start_time = Request::get('start_time');
        $end_time = Request::get('end_time');
        $pid_name = Request::get('pid_name');
        
        $this->assign('start_times', date("Y-m-d"));
        $this->assign('end_times', date("Y-m-d"));
        
        $this->assign('pid_name',$pid_name);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        if ($pid_name){
            $agent_id = Db::name('fd_user')->where('username',$pid_name)->value('id');
        }
        if ($keyword || $phone || $start_time || $end_time || $pid_name){
            if(!empty($keyword)){
                $where['username'] = $keyword;
            }
            if(!empty($phone)){
                $where['phone'] = $phone;
            }
            if(!empty($pid_name)){
                $where['pid'] = $pid_name;
            }
            
            if(!empty($start_time) && !empty($end_time)){
                $s_time = strtotime($start_time);
                $e_time = strtotime($end_time) + 1 * 24 * 3600;
                
                $where['create_time'] = ['>',$s_time];
                $where['create_time'] = ['<',$e_time];
            }
            
            $user_list = Db::name('fd_user')->where($where)->paginate(10,false,['query'=>request()->param()]);
            $page = $user_list->render();  //构造分页
            $this->assign('pages', $page);   //输出分页
            $this->assign('list',$user_list);
        }else{
            $user_list = Db::name('fd_user')->order('id','desc')
                ->paginate(10,false,['query'=>request()->param()]);
            $page = $user_list->render();  //构造分页
            $this->assign('pages', $page);   //输出分页
            $this->assign('list',$user_list);
        }
        User_m::day_order();
        return $this->fetch();
    }

    /**
     * 添加会员
     * @auth true
     * @menu true
     */
    public function add_users()
    {
        if (Request::isPost()){
            $info['username'] = Request::post('username');
            $info['phone'] = Request::post('phone');
            $info['password'] = md5(Request::post('password'));
            $info['payment'] = md5(Request::post('payment'));
            $invite_code = Request::post('invite_code');
            $info['balance'] = Db::name('system_config')->where('name','cjrg')->value('value');
            $add = User_m::add_users($info,$invite_code);
            switch ($add)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
                case 'error_use':
                    $this->error('账户已存在');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑会员
     * @auth true
     * @menu true
     */
    public function edit_users()
    {
        $id = Request::get('id');
        $info = User_m::get_user_info($id);
        $this->assign('info',$info);
        $level_info = User_m::level_select();
        $this->assign('level_info',$level_info);
        if(Request::isPost()){
            $id = Request::post('id');
            $edit_info['role'] = Request::post('role');
            $edit_info['nick_name'] = Request::post('nick_name');
            // $edit_info['balance'] = Request::post('balance');
            // $edit_info['total'] = Request::post('total');
            // $edit_info['financial'] = Request::post('financial');
            // $edit_info['freeze'] = Request::post('freeze');
            // $edit_info['level'] = Request::post('level');
            // $edit_info['finish_order'] = Request::post('finish_order');
            // $edit_info['group_order'] = Request::post('group_order');
            $edit_info['bank_name'] = Request::post('bank_name');
            // $edit_info['holder_name'] = Request::post('holder_name');
            $edit_info['account_number'] = Request::post('account_number');
            $edit_info['phone_number'] = Request::post('phone_number');
            // $edit_info['email'] = Request::post('email');
            // $edit_info['ifsc'] = Request::post('ifsc');
            // $edit_info['upi'] = Request::post('upi');
            // $edit_info['type'] = Request::post('type');
            // $edit_info['address_usdt'] = Request::post('address_usdt');
            // $edit_info['address_name'] = Request::post('address_name');
            // $edit_info['address_phone'] = Request::post('address_phone');
            // $edit_info['address_info'] = Request::post('address_info');
            $password = Request::post('password');
            $payment = Request::post('payment');
            $edit_info['update_time'] = time();
            if ($password){
                $edit_info['password'] = md5($password);
            }
            if ($payment){
                $edit_info['payment'] = md5($payment);
            }
            $edit_users = User_m::edit_users($id,$edit_info);
            switch ($edit_users)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }
    
    
    /**
     * 人工余额操作
     * @auth true
     * @menu true
     */
    public function recharges()
    {
        $id = Request::get('id');
        $info = User_m::get_user_info($id);
        $this->assign('info',$info);
        $level_info = User_m::level_select();
        $this->assign('level_info',$level_info);
        if(Request::isPost()){
            
            $id = Request::post('id');
            
            $money = Request::post('balance');
            $is_status = Request::post('is_status');
            $type = Request::post('type');
            $remarks = Request::post('remarks');
            
            $user_info = Db::name('fd_user')->where('id',$id)->find();
            $infos['uid'] = $user_info['id'];
            $infos['pid'] = $user_info['pid'];
            $infos['username'] = $user_info['username'];
            $infos['phone'] = $user_info['phone'];
            $infos['money'] = $money;
            $infos['service_charge'] = 0;
            $infos['type'] = $type;
            $infos['status'] = 1;
            $infos['remarks'] = $remarks;
            $infos['is_status'] = $is_status;
            $infos['create_time'] = time();
            $recharge_starts = Db::name('fd_recharge')->insert($infos);
            
            if ($recharge_starts == true){
                if($is_status == 1){
                    $recharge_money = Db::name('fd_user')->where('id',$id)->setInc('balance',$money);
                }else{
                    $recharge_money = Db::name('fd_user')->where('id',$id)->setDec('balance',$money);
                }
                
                if ($recharge_money == true){
                        $this->success('充值成功!');
                    }else{
                        $this->error('充值失败!');
                    }
                }else{
                    $this->error('记录有误!');
                }
        }
        return $this->fetch();
    }


    public function delete_user()
    {
        $this->applyCsrfToken();
        $id = input('post.id/d',0);
        $res = Db::name('fd_user')->where('id',$id)->delete();
        if($res)
            $this->success('删除成功!');
        else
            $this->error('删除失败!');
    }

    /**
     * 封禁/解封会员
     * @auth true
     */
    public function edit_users_status()
    {
        $id = input('id/d',0);
        $status = input('status/d',0);
        if(!$id || !$status) return $this->error('参数错误');
        $res = User_m::edit_users_status($id,$status);
        switch ($res)
        {
            case 'success':
                return $this->success('操作成功');
                break;
            case 'error':
                return $this->error('操作失败');
        }
    }

    /**
     * 会员等级
     * @auth true
     */
    public function user_level()
    {
        $id = Request::get('id');
        $info = User_m::get_user_info($id);
        $this->assign('info',$info);
        $level_info = User_m::level_select();
        $this->assign('level_info',$level_info);
        if (Request::isPost()){
            $id = Request::post('id');
            $edit_info['level'] = Request::post('level');
            $edit_info['update_time'] = time();
            
            $level = Db::name('fd_level')->where('id',$edit_info['level'])->find();
            $edit_info['finish_order'] = 0;
            $edit_info['day_max_o'] = $level['order_num'];
            // $edit_users['']
            $edit_users = User_m::user_level($id,$edit_info);
            switch ($edit_users)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 会员分组
     * @auth true
     */
    public function user_group()
    {
        $id = Request::get('id');
        $info = User_m::get_user_info($id);
        $this->assign('info',$info);

        $group_info = User_m::group_info();
        $this->assign('group_info',$group_info);

        if (Request::isPost()){
            $id = Request::post('id');
            $edit_info['group'] = Request::post('group');
            $edit_info['group_order'] = 0;
            $edit_info['update_time'] = time();
            $edit_users = User_m::user_group($id,$edit_info);
            switch ($edit_users)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 会员等级
     * @auth true
     * @menu true
     */

    public function level()
    {
        $list = User_m::level();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加等级
     * @auth true
     * @menu true
     */

    public function add_level()
    {
        if (Request::isPost()){
            $info['name'] = Request::post('name');
            $info['money'] = Request::post('money');
            $info['order_num'] = Request::post('order_num');
            // $info['w_num'] = Request::post('w_num');
            $info['w_min'] = Request::post('w_min');
            $info['w_max'] = Request::post('w_max');
            // $info['level_day'] = Request::post('level_day');
            // $info['w_order_num'] = Request::post('w_order_num');
            $info['create_time'] = time();
            $info['yjbl'] = Request::post('yjbl');
            $info['txsxf'] = Request::post('txsxf');
            $add_level = User_m::add_level($info);
            switch ($add_level)
            {
                case 'success':
                    $this->success('添加成功');
                    break;
                case 'error':
                    $this->error('添加失败');
                    break;
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑等级
     * @auth true
     * @menu true
     */

    public function edit_level($id)
    {
        $level_info = User_m::level_info($id);
        $this->assign('info',$level_info);
        if (Request::isPost()){
            $id = Request::post('id');
            $info['name'] = Request::post('name');
            $info['money'] = Request::post('money');
            $info['order_num'] = Request::post('order_num');
            // $info['w_num'] = Request::post('w_num');
            $info['w_min'] = Request::post('w_min');
            $info['w_max'] = Request::post('w_max');
            // $info['level_day'] = Request::post('level_day');
            // $info['w_order_num'] = Request::post('w_order_num');
            $info['yjbl'] = Request::post('yjbl');
            $info['txsxf'] = Request::post('txsxf');
            $edit_level = User_m::edit_level($id,$info);
            switch ($edit_level)
            {
                case 'success':
                    $this->success('修改成功');
                    break;
                case 'error':
                    $this->error('修改失败');
                    break;
            }

        }
        return $this->fetch();
    }

    /**
     * 删除等级
     * @auth true
     * @menu true
     */

    public function del_level()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            $del_level = User_m::del_level($id);
            switch ($del_level)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'删除成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'删除失败'];
                    break;
            }
        }
    }

    /**
     * 客服管理
     * @auth true
     * @menu true
     */
    public function cs_list()
    {
        $this->title = '客服列表';
        $where = [];
        if(input('tel/s',''))$where[] = ['tel','like','%' . input('tel/s','') . '%'];
        if(input('username/s',''))$where[] = ['username','like','%' . input('username/s','') . '%'];
        if(input('addtime/s','')){
            $arr = explode(' - ',input('addtime/s',''));
            $where[] = ['addtime','between',[strtotime($arr[0]),strtotime($arr[1])]];
        }
        $this->_query('system_cs')
            ->where($where)
            ->page();
    }

    /**
     * 添加客服
     * @auth true
     * @menu true
     */
    public function add_cs()
    {
        if(request()->isPost()){
            $this->applyCsrfToken();
            $username = input('post.username/s','');
            $tel = input('post.tel/s','');
            $pwd = input('post.pwd/s','');
            $qq = input('post.qq/d',0);
            $wechat = input('post.wechat/s','');
            $qr_code = input('post.qr_code/s','');
            $time = input('post.time');
            $arr = explode('-', $time);
            $btime = substr($arr[0],0,5);
            $etime = substr($arr[1],1,5);
            $data = [
                'username'  => $username,
                'tel'       => $tel,
                'pwd'       => $pwd,    //需求不明确，暂时以明文存储密码数据
                'qq'        => $qq,
                'wechat'    => $wechat,
                'qr_code'   => $qr_code,
                'btime'     => $btime,
                'etime'     => $etime,
                'addtime'   => time(),
            ];
            $res = db('xy_cs')->insert($data);
            if($res) return $this->success('添加成功');
            return $this->error('添加失败，请刷新再试');
        }
        return $this->fetch();
    }

    /**
     * 客服封禁/解封
     * @auth true
     */
    public function edit_cs_status()
    {
        $this->applyCsrfToken();
        $this->_save('system_cs', ['status' => input('post.status/d',1)]);
    }

    /**
     * 编辑客服信息
     * @auth true
     * @menu true
     */
    public function edit_cs()
    {
        if(request()->isPost()){
            $this->applyCsrfToken();
            $id = input('post.id/d',0);
            $username = input('post.username/s','');
            $tel = input('post.tel/s','');
            $qq = input('post.qq',0);
            $wechat = input('post.wechat/s','');
            $qr_code = input('post.qr_code/s','');
            $time = input('post.time');
            $arr = explode('-', $time);
            $btime = substr($arr[0],0,5);
            $etime = substr($arr[1],1,5);
            $data = [
                'username'  => $username,
                'tel'       => $tel,
                'qq'        => $qq,
                'wechat'    => $wechat,
                'qr_code'   => $qr_code,
                'btime'     => $btime,
                'etime'     => $etime,
            ];
            $res = db('system_cs')->where('id',$id)->update($data);
            if($res!==false) return $this->success('编辑成功');
            return $this->error('编辑失败，请刷新再试');
        }
        $id = input('id/d',0);
        $this->list = db('system_cs')->find($id);
        return $this->fetch();
    }

    /**
     * 会员充值
     * @auth true
     * @menu true
     */
    public function recharge()
    {
        if (Request::isPost()){
            $id = Request::post('id');
            $money = Request::post('money');
            $recharge = User_m::recharge($id,$money);
            switch ($recharge)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'手动充值成功'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'手动充值失败'];
                    break;
                case 'error_status':
                    return ['code'=>0,'msg'=>'系统错误'];
                    break;
            }
        }
    }
    
    
    public function dan()
    {
        return $this->fetch();
    }
    
    


}